package Final;

import netscape.security.UserTarget;

import javax.swing.*;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Enroll {

    static ResultSet rs;
    static PreparedStatement pstmtt = null;

    public Enroll(){
        JFrame jf = new JFrame();
        jf.setTitle("注册界面");
        jf.setSize(340, 400);
        //jf.setDefaultCloseOperation(3);
        jf.setLayout(new FlowLayout());
        jf.setLocationRelativeTo(null);
        jf.setResizable(false);
        JLabel labname = new JLabel();
        TextField textname = new TextField();
        JLabel labID = new JLabel();
        TextField textID = new TextField();
        JLabel labOrigin = new JLabel();
        TextField textOrigin = new TextField();


        java.awt.FlowLayout fl = new java.awt.FlowLayout(FlowLayout.CENTER, 5, 5);
        jf.setLayout(fl);

        java.awt.Dimension dim2 = new java.awt.Dimension(50, 20);//标签的大小
        java.awt.Dimension dim3 = new java.awt.Dimension(250, 30);//输入框的大小
        java.awt.Dimension dim4 = new java.awt.Dimension(70, 40);//按钮的大小

        //添加“学号”标签

        labname.setText("名字");
        labname.setPreferredSize(dim2);
        jf.add(labname);

        //添加账号输入框，并添加监控事件

        textname.setPreferredSize(dim3);
        jf.add(textname);

        //添加“学号”标签

        labID.setText("管理员id");
        labID.setPreferredSize(dim2);
        jf.add(labID);

        //添加账号输入框，并添加监控事件

        textID.setPreferredSize(dim3);
        jf.add(textID);

        //添加“籍贯”标签

        labOrigin.setText("性别");
        labOrigin.setPreferredSize(dim2);
        jf.add(labOrigin);

        textOrigin.setPreferredSize(dim3);
        jf.add(textOrigin);

        //添加“籍贯”标签

        JLabel labCardID = new JLabel();
        labCardID.setText("账号");
        labCardID.setPreferredSize(dim2);
        jf.add(labCardID);

        TextField textCardID = new TextField();
        textCardID.setPreferredSize(dim3);
        jf.add(textCardID);

        //添加“籍贯”标签

        JLabel labAge = new JLabel();
        labAge.setText("年龄");
        labAge.setPreferredSize(dim2);
        jf.add(labAge);

        TextField textAge = new TextField();
        textAge.setPreferredSize(dim3);
        jf.add(textAge);

        //添加“籍贯”标签

        JLabel labPASSwORD = new JLabel();
        labPASSwORD.setText("密码");
        labPASSwORD.setPreferredSize(dim2);
        jf.add(labPASSwORD);

        TextField textPASSwORD = new TextField();
        textPASSwORD.setPreferredSize(dim3);
        jf.add(textPASSwORD);


        //添加一个登录按钮
        JButton button_login = new JButton();
        button_login.setText("注册");
        button_login.setPreferredSize(dim4);
        button_login.setBounds(10, 300, 300, 40);
        jf.add(button_login);

        JButton button_cancle = new JButton();
        button_cancle.setText("取消");
        button_cancle.setPreferredSize(dim4);
        button_cancle.setBounds(10, 300, 300, 40);
        jf.add(button_cancle);

        jf.setVisible(true);

        //if(textID.getText() == )

        button_cancle.addActionListener(e -> {jf.dispose();});

        button_login.addActionListener(e -> {
            try {
                Connection conn = Sql.open();
                String sql = "select * from admin where ID = ?";
                pstmtt = conn.prepareStatement(sql);
                pstmtt.setString(1,textID.getText());
                rs = pstmtt.executeQuery();
                if(rs.next()){
                    System.out.println("a");
                    if(textID.getText().equals(rs.getString("ID") )){
                        System.out.println("a");
                        String sql2 = "update admin set name=?,sex=?,age=?,Account=?,password=? where ID=?";
                        Connection conn2 = Sql.open(); //连接数据库
                        try {
//动态操作sql语句
                            PreparedStatement pstmt = conn2.prepareStatement(sql2);
                            pstmt.setString(1, textname.getText());
                            pstmt.setString(2, textOrigin.getText());
                            pstmt.setInt(3, Integer.parseInt(textAge.getText()));
                            pstmt.setString(6, textID.getText());
                            pstmt.setString(4, textCardID.getText());
                            pstmt.setString(5, textPASSwORD.getText());

                            pstmt.executeUpdate();
                            System.out.println(textname.getText());
                            System.out.println(Integer.parseInt(textAge.getText()));
                            System.out.println(textOrigin.getText());
                            System.out.println(textCardID.getText());
                            System.out.println(textPASSwORD.getText());
                            System.out.println(textID.getText());

                        } catch (SQLException h) {

                            h.printStackTrace();

                        } finally {

                            Sql.close(conn2); //关闭数据库

                        }
                        JFrame jFrame = new JFrame();
                        jFrame.setTitle("成功界面");
                        jFrame.setSize(340,100);
                        //jFrame.setDefaultCloseOperation(3);
                        jFrame.setLayout(new FlowLayout());
                        jFrame.setLocationRelativeTo(null);
                        jFrame.setResizable(false);

                        JLabel jLabel = new JLabel("注册成功！");
                        JButton jButton = new JButton("返回");

                        jButton.addActionListener(g -> {

                            jFrame.dispose();

                        });

                        jFrame.add(jLabel);
                        jFrame.add(jButton);
                        jFrame.setVisible(true);
                    }

                }else{
                    new tips();
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

        });
    }



    class tips{


        public tips(){
            JFrame jFrame = new JFrame();
            jFrame.setTitle("提示界面");
            jFrame.setSize(340, 100);
            jFrame.setLayout(new FlowLayout());
            jFrame.setLocationRelativeTo(null);
            jFrame.setResizable(false);

            JLabel jLabel = new JLabel();
            jLabel.setText("此管理员ID无权限！");

            JButton jButton = new JButton();
            jButton.setText("返回");

            jFrame.add(jLabel);
            jFrame.add(jButton);
            jFrame.setVisible(true);

            jButton.addActionListener(e -> {

                jFrame.dispose();

            });
        }

    }

}
